# class base for controller
from google.appengine.ext import webapp

# needed for display
from tools import size_manager

class Select(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the size key
        language_key = self.request.get('language_key')

        # get the key to select
        size_key = self.request.get('size_key')

        #
        si_manager = size_manager.SizeManager(
                language_key=language_key, size_key=size_key)


        # get the key to edit
        select_size_key = self.request.get('select_size_key')
        size = si_manager.get_size(select_size_key)

        # get the current action
        action = self.request.get('action')

        # get the current page_id
        page_id = self.request.get('page_id')

        # back to the crre t page
        return self.redirect('/%s?language=%s&size=%s&page_id=%s' % (
            action, si_manager.language.locale_id, size.width, page_id))

class Edit(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the size key
        language_key = self.request.get('language_key')
        size_key = self.request.get('size_key')
        #
        si_manager = size_manager.SizeManager(
                language_key=language_key, size_key=size_key)

        # get the key to edit
        size_name = self.request.get('size_name')

        # display the size to edit
        template = si_manager.edit(size_name=size_name)
        return self.response.out.write(template)

class Save(webapp.RequestHandler):
    '''
    '''

    def post(self):
        '''
        '''
        # get the size db obj
        size_key = self.request.get('size_key')

        # the key to edit
        size_to_edit_key = self.request.get('size_to_edit_key')

        # get the size key
        language_key = self.request.get('language_key')
        #
        si_manager = size_manager.SizeManager(
                language_key=language_key, size_key=size_key)

        # delete
        if not self.request.get('delete') == '':
            # get the new key to edit
            si_manager.delete(size_to_edit_key)

            # go to the index page
            return self.redirect('/?language=%s&size=%s' % (
                si_manager.language.locale_id, si_manager.size.width))

        # cancel
        elif not self.request.get('new') == '':
            # get the new key to edit
            size_to_edit_key = si_manager.new_size()

        # cancel
        elif not self.request.get('cancel') == '':
            # go to the index page
            return self.redirect('/?language=%s&size=%s' % (
                si_manager.language.locale_id, si_manager.size.width))

        # we save and display the size
        else:
            # get name
            name = self.request.get('name')

            # get width
            width = self.request.get('width')
            if not width or width == '':
                width = '1024'

            width = int(width)

            # get default
            default = self.request.get('default') == 'on'

            # tools save the size
            si_manager.save(
                    size_to_edit_key, name, width, default)

        # display the size to edit
        template = si_manager.edit(size_key=size_to_edit_key)
        return self.response.out.write(template)
